﻿using System;
using System.Collections.Generic;
using System.Text;

namespace RBAC.SqlCache
{
    /// <summary>
    /// 带缓存删除的类,继承此类即有2个缓存删除的方法
    /// </summary>
    internal class RemoveCaches
    {
        /// <summary>
        /// 缓存key的头部
        /// </summary>
        internal string basicKey;

        /// <summary>构造函数,请传入basicKey
        /// </summary>
        /// <param name="basicKey"></param>
        public RemoveCaches(string basicKey)
        {
            this.basicKey = basicKey + "@";
        }

        #region 特殊缓存处理

        /// <summary>删除key中带"Model"的缓存
        /// </summary>
        /// <param name="Id"></param>
        internal void RemoveModelCache(int Id)
        {
            Caches.TryRemoveCache(ModelAddKey(Id.ToString()));
        }

        /// <summary>删除key中带"List"的缓存
        /// </summary>
        internal void RemoveListCache()
        {
            Caches.RemoveMultiCache(basicKey + "List_");
        }

        /// <summary>删除key中带有Count_的缓存
        /// </summary>
        internal void RemoveCountCache()
        {
            Caches.RemoveMultiCache(CountAddKey(""));
        }

        /// <summary>删除指定表中的所有缓存
        /// </summary>
        internal void RemoveAll(string tableName)
        {
            Caches.RemoveMultiCache(tableName + "@");
        }

        /// <summary>删除所有的本表缓存
        /// </summary>
        internal void RemoveAll()
        {
            Caches.RemoveMultiCache(this.basicKey);
        }

        #endregion

        #region 获取键值

        /// <summary>获取总数的键值
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        internal string CountAddKey(string sqlString)
        {
            return this.basicKey + "Count_" + sqlString;
        }

        /// <summary>获取列表的键值
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        internal string ListAddKey(string sqlString)
        {
            return this.basicKey + "List_" + sqlString;
        }

        /// <summary>获取表的键值
        /// </summary>
        /// <param name="sqlString"></param>
        /// <returns></returns>
        internal string ModelAddKey(string id)
        {
            return this.basicKey + id;
        }

        #endregion

        #region 权限缓存的控制

        /// <summary> 删除Key中带Competence的权限数组
        /// </summary>
        internal void RemoveCompetence(int roleId)
        {
            Caches.TryRemoveCache("Competence_" + roleId.ToString());
        }

        /// <summary>删除key中带"RemoveCompetence_"的缓存
        /// </summary>
        internal void RemoveAllCompetence()
        {
            Caches.RemoveMultiCache("Competence_");
        }

        /// <summary>获取权限的键值
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        internal string CompetenceAddKey(int roleId)
        {
            return "Competence_" + roleId.ToString();
        }

        #endregion

    }
}
